<template>
  <!--弹窗-->
  <el-dialog
    width="90%"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose">
    <div class="pad20">
      <div>
        <el-form :inline="true" size="mini">
          <el-form-item label="课程名称">
            <el-input v-model="map.courseName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon='el-icon-search' type="primary" @click="handleCheck">查询</el-button>
            <el-button icon='el-icon-refresh' class="filter-item" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-table v-loading="ctrl.load" size="medium" :data="list" stripe border style="width: 100%">
          <el-table-column type="index" label="序号">
          </el-table-column>
          <el-table-column prop="course_name" label="课程名称">
          </el-table-column>
          <el-table-column label="课程分类" width="180">
            <template slot-scope="scope">
              {{scope.row.yj}}/{{scope.row.ej}}
            </template>
          </el-table-column>
          <el-table-column label="是否收费" width="90">
            <template slot-scope="scope">
              <span v-if="scope.row.course_price - 0.0 < 1e-5">免费</span>
              <span v-else>收费</span>
            </template>
          </el-table-column>
          <el-table-column label="原价" width="100">
            <template slot-scope="scope">
              {{scope.row.course_price.toFixed(2)}}
            </template>
          </el-table-column>
          <el-table-column label="状态" width="90">
            <template slot-scope="scope">
              <span>{{textIsStatus[scope.row.status]}}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="100">
            <template slot-scope="scope">
                <span v-if="null != scope.row.zid">
                  <el-link disabled>已添加</el-link>
                </span>
                <span v-else>
                  <el-button type="success" @click="handleChoice(scope.row.kid)" size="mini">选择</el-button>
                </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div>
        <el-pagination
          background
          style="float: right;margin-top: 20px; margin-bottom: 22px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-size="page.size"
          :page-sizes="[2,3,4]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
      <br/>
      <br/>
    </div>
  </el-dialog>
</template>
<script>
  // import * as api from '@/api/homepage'
  // import * as courseApi from '@/api/course'
  export default {
    data() {
      return {
        map: {},
        list: [],
        ctrl: {
          load: false
        },
        textIsStatus: {
          0: '上架',
          1: '下架',
          2: '待审核',
          3: '审核成功',
          4: '审核失败',
        },
        page: {
          current: 1,
          size: 2,
          total: 0
        },
        formData: {}
      }
    },
    props: {
      zid: {
        type: String,
        default: ''
      },
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    watch: {
      visible: function(val) {
        if (val) {
          setTimeout(() => {
            this.map.zid = this.zid
            this.pageList()
          }, 500)
        }
      }
    },
    methods: {
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`)
        this.page.size = val
        this.page.current = 1
        this.pageList()
      },
      handleCurrentChange(val) {
        this.page.current = val
        this.pageList()
        // console.log(`当前页: ${val}`)
      },
      // 查询条件
      handleCheck() {
        this.page.current = 1
        this.pageList()
      },
      // 重置查询条件
      handleReset() {
        this.page.current = 1;
        this.reload()
      },
      // 分页列出课程信息
      pageList() {
        this.ctrl.load = true
        this.$axios.get(`education-system/sys/zonecourse/courseList?page=${
          this.page.current}&limit=${this.page.size}&courseName=${
          this.map.courseName}&zid=${
          this.map.zid}`).then(d => {
          // console.log(d);
          this.list = d.data.obj.records;
          this.page.total = d.data.obj.total;
          this.ctrl.load = false
        }).catch(() => {
          this.ctrl.load = false
        })
      },
      // 刷新当前页面
      reload() {
        this.map = {}
        this.map.zid = this.zid
        this.pageList()
      },
      handleChoice(res) {
        this.ctrl.loading = true
        this.map.kid = res
        this.$axios.get(`education-system/sys/zonecourse/zoneCourseSave?kid=${this.map.kid}&zid=${this.map.zid}`).then(r=>{
          this.ctrl.loading=false
          if(r.status == 200 && r.data.obj == true){
            this.$message({
              type: 'success',
              message: "保存成功"
            });
            // 提交成功, 关闭窗口, 刷新列表
            this.handleClose('close-callback')
          }else{
            this.$message({
              showClose: true,
              message: '提交失败',
              type: 'error'
            });
          }
        }).catch(() => {
          this.ctrl.loading=false
        })
      },
      handleClose() {
        this.$emit('close-callback')
      },
      textClass(isFree) {
        return {
          c_red: isFree === 0,
          c_blue: isFree === 1
        }
      }
    }
  }
</script>
<style scoped>
  .cancel {
    text-align: right;
  }
  .button {
    padding: 5px 10px;
  }
</style>
